//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.zhilei.untils;

import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.util.Base64;
import java.util.Date;

@Slf4j
@Component
@RefreshScope
public class JWTUtils {

    public static final String AT = "@";

    @Autowired
    private JWTProperties jwtProperties;

    @Value("${jwt.key}")
    private String JWT_KEY;

    public JWTUtils() {
    }

    public String createJWT(String body, Long expirationTime, String prefix) {
        if (expirationTime == null) {
            GraceException.disPlay(ResponseStatusEnum.SYSTEM_NO_EXPIRE_ERROR);
        }

        String jwtToken = this.dealJWT(body, expirationTime);
        return prefix + AT + jwtToken;
    }

    public String createJWT(String body, String prefix) {
        String jwtToken = this.dealJWT(body, null);
        return prefix + AT + jwtToken;
    }

    public String dealJWT(String body, Long expirationTime) {

        String key = this.JWT_KEY;
        log.error("key: {}", key);

        String base64 = Base64.getEncoder().encodeToString(key.getBytes());

        SecretKey secretKey = Keys.hmacShaKeyFor(base64.getBytes());
        String jwtToken;
        if (expirationTime == null) {
            jwtToken = this.generateJWT(body, secretKey);
        } else {
            jwtToken = this.generateJWT(body, secretKey, expirationTime);
        }

        log.info("jwt token: {}", jwtToken);
        return jwtToken;
    }

    private String generateJWT(String body, SecretKey secretKey) {
        String jwtToken = Jwts.builder().setSubject(body).signWith(secretKey).compact();
        return jwtToken;
    }

    private String generateJWT(String body, SecretKey secretKey, Long expirationTime) {
        Date expirationDate = new Date(System.currentTimeMillis() + expirationTime);
        String jwtToken = Jwts.builder().setSubject(body).signWith(secretKey).setExpiration(expirationDate).compact();
        return jwtToken;
    }

    /**
     * 处理jwt
     *
     * @param pendingJWT
     * @return
     */
    public String checkJWT(String pendingJWT) {
        String key = this.JWT_KEY;
        log.error("key: {}", key);
        String base64 = Base64.getEncoder().encodeToString(key.getBytes());
        SecretKey secretKey = Keys.hmacShaKeyFor(base64.getBytes());
        JwtParser jwtParser = Jwts.parserBuilder().setSigningKey(secretKey).build();
        Jws<Claims> claimsJws = jwtParser.parseClaimsJws(pendingJWT);
        String body = claimsJws.getBody().getSubject();
        return body;
    }
}
